import {
  getItem,
  setItem
} from "../utils/storage"
import axios from "axios";



// 创建axios实例
export const service = axios.create({
  // baseURL: process.env.BASE_URL, // api的base_url
  // withCredentials: true
  timeout: 15000, // 请求超时时间2
});
// request拦截器
service.interceptors.request.use(
  (config) => {
    // console.log( config,1111);
    const token = getItem('token')

    if (token) { //- 置令牌请求头
      config.headers["Authorization"] = `basic ${token}`;
     
    }
    // config.headers['Content-Type'] =`application/x-www-form-urlencoded`
    // console.log(config, 'configconfigconfigconfigconfigconfig');
    return config;
  },
  error => {
    Promise.reject(error);
  }
);
// respone拦截器

service.interceptors.response.use(
  response => {
    // console.log(response, 'response');
    const res = response.data;

    return res;

    // if (res.Code === 200 || res.Code === 0 || res.code === 1001) {
    //   return res;
    // } else {


    //   uni.showToast({
    //     title: res.Message,
    //     icon: "none"
    //   });
    //   return Promise.reject(res);
    // }
  },
  error => {
    uni.showToast({
      title: error.message,
      icon: "none"
    });
    return Promise.reject(error);
  }
);

//- 自己定义个适配器，用来适配uniapp的语法
axios.defaults.adapter = function (config) {
  return new Promise((resolve, reject) => {
    var settle = require('axios/lib/core/settle');
    var buildURL = require('axios/lib/helpers/buildURL');
    var buildFullPath = require('axios/lib/core/buildFullPath');
    let fullurl = buildFullPath(config.baseURL, config.url)
    // console.log(config,99999999999999999999)
    uni.request({
     
      method: config.method.toUpperCase(),
      url: buildURL(fullurl, config.params, config.paramsSerializer),
      headers:{'Content-Type':'application/x-www-form-urlencoded'},
      data: config.data,
      dataType: config.dataType,
      responseType: config.responseType,
      sslVerify: config.sslVerify,
      complete: function complete(response) {
        response = {
          data: response.data,
          status: response.statusCode,
          errMsg: response.errMsg,
          header: response.header,
          config: config
        };

        settle(resolve, reject, response);
      }
    })
  })
}
// axios.defaults.adapter = function (config) {
//   return new Promise((resolve, reject) => {
//     var settle = require('axios/lib/core/settle');
//     var buildURL = require('axios/lib/helpers/buildURL');
//     uni.request({
//       method: config.method.toUpperCase(),
//       url: buildURL(config.url, config.params, config.paramsSerializer),
//       header: config.headers,
//       data: config.data,
//       dataType: config.dataType,
//       responseType: config.responseType,
//       sslVerify: false,
//       complete: function complete(response) {
//         response = {
//           data: response.data,
//           status: response.statusCode,
//           errMsg: response.errMsg,
//           header: response.header,
//           config: config
//         };

//         settle(resolve, reject, response);
//       }
//     })
//   })
// }

/**
 * @description: 
 * @param {string} method   // 请求方式
 * @param {*} url
 * @param {*} data
 * @return {*}
 */
export function request(method, url, data) {
  //暴露 request 给我们好API 管理
  method = method.toLocaleLowerCase(); //封装RESTful API的各种请求方式 以 post get delete为例

  // console.log(data,'datadatadata');
  switch (method) {
    case 'postfile':
      return service.post(url, data
        // {headers: { 'Authorization': `dsly ${'xxxxxxxxxxxxxxxxxxxxxxxxx'}`} }
      );
    case 'post':
      return service.post(url, data,{
        
      }); //axios的post 默认转化为json格式
    case 'get':
      return service.get(url, {
        params: data
      });
    case 'delete':
      return service.delete(url, {
        data: data
      });
    case 'put':
      return service.put(url, data);
    case 'patch':
      return service.patch(url, data);
    default:
      break;
  }
}